import * as api from '../../utils/api.js';
import * as util from '../../utils/util.js';

Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    groupArray: ['shelfPrice asc', 'shelfPrice desc',
      'salesVolume asc', 'salesVolume desc', 'createTime asc', 'createTime desc'
    ],
    list_goods: [],
    allPage: 0,
    applyAge: '',
    startPrice: '',
    brand: '',
    pageSize: '4',
    endPrice: '',
    type: '',
    keyWord: '',
    group: 'shelfPrice asc',
    curPage: 0,
    price: 0, //价格排序   0升序  1desc降序
    salesVolume: 0, //销售量排序
    createTime: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {

  },

  onShow: function() {
    console.log(this.data.curPage + "=====" + this.data.allPage)

    this.fetchArticleList(0);

  },
  onLoad: function() {

  },
  //选择点击事件
  selectChange: function(e) {
    var text = e._relatedInfo.anchorTargetText;
    switch (text) {
      case '价格':
        this.data.price = ((this.data.price == 0) ? 1 : 0);
        this.data.group = ((this.data.price == 0) ? this.data.groupArray[0] : this.data.groupArray[1]);
        this.data.salesVolume = 0;
        this.data.createTime = 0;
        break;
      case '销量':
        this.data.salesVolume = ((this.data.salesVolume == 0) ? 1 : 0);
        this.data.group = ((this.data.salesVolume == 0) ? this.data.groupArray[2] : this.data.groupArray[3]);
        this.data.price = 0;
        this.data.createTime = 0;
        break;
      case '上架时间':
        this.data.createTime = ((this.data.createTime == 0) ? 1 : 0);
        this.data.group = ((this.data.createTime == 0) ? this.data.groupArray[4] : this.data.groupArray[5]);
        this.data.price = 0;
        this.data.salesVolume = 0;
        break;
    }
    this.fetchArticleList(0);
  },

  itemClick: function(e) {

    var data = e.currentTarget.dataset.variable;
    console.log(data);
    // wx.navigateTo({
    //   url: data.pathsrc + "?json=" + JSON.stringify(e.currentTarget.dataset.variable),
    // })
  },

  //上拉加载更多
  onReachBottom() {
    var that = this;
    var noPage = this.data.curPage;

    // 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
    if (noPage < this.data.allPage) {
      this.fetchArticleList(noPage + 1)
    } else {
      console.log("dsdsafafdddddddddddddddddddddddddddddd=====" )
    }
   },

  //下拉刷新
  onPullDownRefresh() {
    this.setData({
      curPage: 0,
    })
    this.fetchArticleList(0);
  },

  // 数据进行网络请求
  fetchArticleList(pageNo) {
    var that = this;
    var requestData = {
      group: that.data.group,
      curPage: pageNo,
      applyAge: that.data.applyAge,
      startPrice: that.data.startPrice,
      brand: that.data.brand,
      pageSize: that.data.pageSize,
      endPrice: that.data.endPrice,
      type: that.data.type,
      keyWord: that.data.keyWord,
    }
  
    var networkData = JSON.stringify(requestData);
    var param = {
      params: networkData,
    }

    api.request1("goods/app/list", param, function(result) {
      //数据请求成功，缓存用户信息数据
      if (that.data.curPage = 0) {
        wx.stopPullDownRefresh();
      }
      var pages = 0;
      if (result.data.pages > 0) {
        pages = result.data.pages - 1;
      }
      var list = that.data.list_goods.concat(result.data.list);
      that.setData({
        list_goods: pageNo == 0 ? result.data.list : list,
        allPage: pages,
        curPage: pageNo,
      })

    }, function(error) {
      //失败
      console.log(error);
      if (that.data.curPage = 0) {
        wx.stopPullDownRefresh();
      }
    })
  },

})